<template>
  <div class="container">
    <el-row type="flex">
      <el-col :span="12">
        <el-image :src="loginBgUrl" fit="fit"></el-image>
      </el-col>
      <el-col :span="12">
        <div class="login">
          <div class="title">
            <span>系统登录</span>
          </div>
          <el-form
              :model="loginForm"
              :rules="loginFormRules"
              ref="loginForm"
              @submit.native.prevent
          >
            <el-form-item prop="userName">
              <el-input v-model="loginForm.userName" placeholder="请输入用户名" clearable>
                <template slot="prepend">
                  <i class="el-icon-s-custom"></i>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                  type="password"
                  v-model="loginForm.password"
                  placeholder="请输入密码"
                  show-password
                  clearable
              >
                <template slot="prepend">
                  <i class="el-icon-lock"></i>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="captcha">
              <el-input
                  v-model="loginForm.captcha"
                  placeholder="请输入验证码"
                  clearable
                  @keyup.enter.native="handlerLogin"
              >
                <template slot="prepend">
                  <i class="el-icon-s-check"></i>
                </template>
                <template slot="append">
                  <captcha/>
                </template>
              </el-input>
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="handlerLogin">登录</el-button>
          <div class="footer">
            <el-link
                type="primary"
                :underline="false"
                @click="handlerResetPassword"
            >忘记密码？
            </el-link>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Captcha from '@/components/login/Captcha'
import _ from 'lodash'

export default {
  name: 'Login',
  components: { Captcha },
  data () {
    const captchaValidate = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      }

      if (value !== this.captcha) {
        callback(new Error('请输入正确的验证码'))
      }
      callback()

    }
    return {
      loginBgUrl: require('@/assets/images/login-bg.jpg'),
      identifyCode: '',
      identifyCodes: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      loginForm: {
        userName: '',
        password: '',
        captcha: ''
      },
      loginFormRules: {
        userName: {
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        },
        password: {
          required: true,
          message: '请输入正确的密码',
          trigger: 'blur'
        },
        captcha: {
          required: true,
          validator: captchaValidate,
          trigger: 'blur'
        },
      }
    }
  },
  computed: {
    captcha () {
      return this.$store.getters['login/getCaptcha']
    }
  },
  methods: {
    handlerResetPassword () {
      // todo 忘记密码
    },
    handlerLogin () {
      console.log('开始登录了')
      this.$refs.loginForm.validate((valid) => {
        if (!valid) {
          setTimeout(() => {
            var isError = document.getElementsByClassName('is-error')
            isError[0].querySelector('input').focus()
          }, 100)
          return false
        }

        const userInfo = _.cloneDeep(this.loginForm)
        delete userInfo.captcha
        this.$store.commit('login/setUserInfo', userInfo)
        this.$router.push({ name: 'home' })
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  background: #2c2c54;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-row {
    width: 800px;
    height: 400px;
    border-radius: 0.5rem;
    padding: 0;
    background: #fff;

    .el-col {
      .el-image {
        width: 100%;
        height: 100%;
        background-size: 100% 100%;
        border-top-left-radius: 0.5rem;
        border-bottom-left-radius: 0.5rem;
      }

      .login {
        padding: 1rem;
        box-sizing: border-box;

        .title {
          height: 4.5rem;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;

          span {
            font-size: 1.5rem;
            letter-spacing: 0.5rem;
          }
        }

        .el-input /deep/ .el-input-group__append {
          padding: 0 !important;
          border: none;
        }

        .el-button {
          width: 100%;
          letter-spacing: 1rem;
        }

        .footer {
          padding: 1rem;
        }
      }
    }
  }
}
</style>
